home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- * *
- * UNBACKUP.C -- MSC Code to permit restoration of DOS backup diskettes *
- * in no particular order *
- * -- Author: Mike Mitchell, Los Alamos National Labs, *
- * Los Alamos, NM 87545 MS D432 *
- * -- Date: July, 1986 *
- * *
- * This code is being placed into the public domain for anyone to use. I *
- * only request that my name appears as author. Modifications are *
- * encouraged, and if really slick, I would like to see them... *
- * *
- *************************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <signal.h>
-
- #define BIGBUF 10000
-
- int no_ignore_path = 0;
- int over_write = 0;
- int verbose = 0;
- int broken_file = 0;
- char *inbuf, *outbuf;
- char cur_dir[128];
-
- struct back_hdr {
- char back_magic[2];
- char back_null[3];
- char back_name[78];
- char back_what[1];
- char back_remains[43];
- };
-
- struct back_hdr hdr;
-
- main(argc,argv)
- char **argv;
- {
- int handler();
-
- if (argc < 2) {
- printf("Usage: unbackup [-sov] d:file.ext\n");
- printf("Example: unbackup -v a:*.exe\n");
- printf(" unbackup -s a:*.*\n");
- exit(1);
- }
-
- getcwd(cur_dir,127);
-
- if (signal(SIGINT, handler) == (int(*)())-1) {
- printf("unbackup: cannot set SIGINT\n");
- abort();
- }
-
- for (argv++, argc--; argc > 0; argv++, argc--)
-
- switch (**argv) {
-
- case '-':
- case '/':
- parse_switch(++*argv);
- break;
-
- default:
- un_backup(*argv);
- break;
- }
- chdir(cur_dir);
- return(broken_file);
- }
-
- handler()
- {
- printf(">>INTERUPT<<\n");
- fcloseall();
- chdir(cur_dir);
- exit(0);
- }
-
- parse_switch(switch_string)
- char *switch_string;
- {
- for ( ; *switch_string != '\0' ; switch_string++)
-
- switch (tolower(*switch_string)) {
-
- case '-':
- case '/':
- break;
-
- case 's':
- no_ignore_path++;
- break;
-
- case 'o':
- over_write++;
- break;
-
- case 'v':
- verbose++;
- break;
-
- default:
- printf("unbackup: switch ignored %c\n",
- *switch_string);
- break;
- }
- }
-
- un_backup(file_spec)
- char *file_spec;
- {
- FILE *in, *out;
- char ch;
- char *out_name, *out_path;
- char *base_name(), *path_name();
-
- if ((in = fopen(file_spec, "rb")) == NULL) {
-
- printf("unbackup: cannot open file %s\n",
- file_spec);
- return;
- }
-
- if (check_header_bad(in)) {
-
- printf("unbackup: incorrect header in %s\n",
- file_spec);
- fclose(in);
- return;
- }
-
- out_name = base_name(hdr.back_name);
- out_path = path_name(hdr.back_name);
-
- if (no_ignore_path) {
-
- make_path(out_path);
- chdir(out_path);
- }
-
- if (access(out_name, NULL) == NULL && !over_write) {
- printf("appending to %s\n", out_name);
- out = fopen(out_name, "ab");
- }
- else {
- printf("writing new to %s\n", out_name);
- out = fopen(out_name, "wb");
- }
-
- inbuf = malloc(BIGBUF);
- outbuf = malloc(BIGBUF);
- setbuf(in, inbuf);
- setbuf(out, outbuf);
-
- if (verbose)
- printf("%s%c%s\n", (no_ignore_path ? out_path : ""),
- (no_ignore_path ? '\\' : '\0'), out_name);
-
- fread(&ch, sizeof(char), 1, in);
-
- while (!feof(in)) {
-
- fwrite(&ch, sizeof(char), 1, out);
- fread(&ch, sizeof(char), 1, in);
- }
-
- free(inbuf);
- free(outbuf);
- free(out_name);
- free(out_path);
- fclose(out);
- fclose(in);
- }
-
- check_header_bad(fp)
- FILE *fp;
- {
- if (fread(&hdr, sizeof(struct back_hdr), 1, fp) != 1)
-
- return(~NULL);
-
- if ((((hdr.back_magic[0] & 0xff) == 0xff) &&
- ((hdr.back_magic[1] & 0xff) >= 0x01)) &&
- (((hdr.back_null[0] & 0xff) == NULL) &&
- ((hdr.back_null[1] & 0xff) == NULL) &&
- ((hdr.back_null[2] & 0xff) == NULL)))
-
- return(NULL);
-
- if ((((hdr.back_magic[0] & 0xff) == 0x00) &&
- ((hdr.back_magic[1] & 0xff) >= 0x01)) &&
- (((hdr.back_null[0] & 0xff) == NULL) &&
- ((hdr.back_null[1] & 0xff) == NULL) &&
- ((hdr.back_null[2] & 0xff) == NULL))) {
-
- broken_file = 39;
- return(NULL);
- }
-
- return(~NULL);
- }
-
- make_path(path)
- char *path;
- {
- char *path_b, *path_p;
- char *base_name(), *path_name();
-
- if (chdir(path) == NULL)
-
- return;
-
- if (*path == '\0') {
-
- chdir("\\");
- return;
- }
-
- path_b = base_name(path);
- path_p = path_name(path);
-
- make_path(path_p);
- chdir(path_p);
- mkdir(path_b);
-
- free(path_p);
- free(path_b);
- }
-
- char *base_name(file_spec)
- char *file_spec;
- {
- char *cp;
-
- cp = malloc(strlen(file_spec) + 1);
- strcpy(cp, file_spec);
-
- for ( ; *cp != '\0' ; cp++)
- ;
-
- for ( ; *cp != '\\' && cp >= file_spec; cp--)
- ;
-
- cp++;
- return (cp);
- }
-
- char *path_name(file_spec)
- char *file_spec;
- {
- char *cp, *cp2;
-
- cp = malloc(strlen(file_spec) + 1);
- strcpy(cp, file_spec);
-
- for (cp2 = cp ; *cp2 != '\0' ; cp2++)
- ;
-
- for ( ; *cp2 != '\\' && cp2 >= cp ; cp2--)
- ;
-
- *cp2 = '\0';
- return(cp);
- }
-